package creator.ui.controller;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.net.URL;

public class ReadData {

  public static Object ReadObjectFromLocal(String path) {
    Object obj = null;
    try {
      String uri = ReadData.class.getResource(path).getPath();

      File file = new File(
        uri
          .replace("file:/", "")
          .replace("FlappyBird.jar!/", "")
          .replace("file:\\", "")
          .replace("FlappyBird.jar!\\", "")
          .substring(1)
      );

      ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));
      obj = in.readObject();
      in.close();
    } catch (IOException e) {
      WriteData.WriteObjectInLocal(null, path);
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    return obj;
  }
}
